const Base = require('./base')
const querystring = require('querystring')
module.exports = class extends Base {
  async instance (id) {
    const account = await this.get_authorizer_by_id(id)
    this.appid = account.appid
    return !((!account || !account.authorized))
  }
  /**
     * 获取授权链接
     * @param redirect
     * @param state
     * @param scope
     * @returns {string}
     */
  get_oauth_url (redirect, state, scope) {
    const url = 'https://open.weixin.qq.com/connect/oauth2/authorize'
    const info = {
      appid: this.appid,
      redirect_uri: redirect,
      response_type: 'code',
      scope: scope || 'snsapi_base',
      state: state || '',
      component_appid: this.conf.appid
    }
    return url + '?' + querystring.stringify(info) + '#wechat_redirect'
  }

  /**
     * 获取web网页授权链接
     * @param redirect
     * @param state
     * @param scope
     * @returns {string}
     */
  get_web_oauth_url (redirect, state, scope) {
    const url = 'https://open.weixin.qq.com/connect/qrconnect'
    const info = {
      appid: 'wx4060b3f1d64b0a62',
      redirect_uri: redirect,
      response_type: 'code',
      scope: scope || 'snsapi_login',
      state: state || '',
      component_appid: this.conf.appid
    }
    return url + '?' + querystring.stringify(info) + '#wechat_redirect'
  }

  /**
     * 获取网页授权token
     * @param appid
     * @param code
     * @returns {*}
     */
  async get_oauth_access_token (appid, code) {
    const component_access_token = await think.cache('wechat_component_access_token')
    const url = `https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=${appid}&code=${code}&grant_type=authorization_code&component_appid=${this.conf.appid}&component_access_token=${component_access_token}`
    const d = await yy.http.get(url)
    yy.log.info('get_oauth_access_token', JSON.stringify(d))
    return d
  }

  /**
     * 刷新网页授权token
     * @param appid
     * @param refresh_token
     * @returns {*}
     */
  async refresh_oauth_access_token (appid, refresh_token) {
    const component_access_token = await this.get_access_token(appid)
    const url = `https://api.weixin.qq.com/sns/oauth2/component/refresh_token?appid=${appid}&grant_type=refresh_token&component_appid=${this.conf.appid}&component_access_token=${component_access_token}&refresh_token=${refresh_token}`
    return yy.http.get(url)
  }

  /**
     * 获取用户简介
     * @param access_token
     * @param openid
     * @returns {*}
     */
  get_userinfo (access_token, openid) {
    const url = `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}&lang=zh_CN`
    return yy.http.get(url)
  }

  /**
     * 获取网站应用用户
     * @param code
     * @param appid
     * @param secret
     * @returns {*}
     */
  async web_get_userinfo (code, appid, secret) {
    const url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`
    yy.log.debug(url)
    const {access_token, openid} = await yy.http.get(url)
    const userInfoUrl = `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}`
    return await yy.http.get(userInfoUrl)
  }
}
